/**********************************************************************************************************
	   Goal: Analysis about Market Competition and Demand for Skills in a Credence Goods Market
       Author: Paiou Wu  wupaiou@pku.edu.cn
       Lasted edited: 2020/04/01
	   Step :
		   Step 1:  Summary statistics
		   Step 2:  Regression
		   
       Input: Analysis.dta
       Notes: 
			Dependent Variables: 1)Training Combined: part 2)Face-to-Face Training:part_C; 3)Web-Based Training: part_O
			Independent Variables: 
			       Competition variables: 1)CountDoc(regression:lnCount); 2)ShareCol; 3)ShareAge 
				   Control variables: 
				       1) Clinician characteristics 
					       Clinician male:doc_male; 
						   Clinician minority:doc_minority; 
						   Clinician age:doc_age;
						   Clinician Education level:doc_edulev_1;doc_edulev_2;doc_edulev_3;doc_edulev_4;
						   Full-time formal medical education(yes=1):doc_ForEdu;
						   Use of Internet(yes=1): doc_UseInt;
						   Full time or not(yes=1):doc_FullTime	;
		                   Share of work time spending on public health service: doc_PubSer;
						   Average daily income:doc_inc(regression:lninc);
						2)Clinic characteristics  
						   Number of permanent residents within 5 kilometers of village clinic :fac_Res;   
						   Distance to township hospital: dis_townhos(regression:lndis);
						3)Village characteristics
						   Daily wage of unskilled 50 year-old male worker:	vil_DailyWage(regression:lnW)
***********************************************************************************************************/
 /*Paiou Wu's path*/ 
	clear all
	set maxvar  10000

	gl final_cleandata         "D:\Prof.Yi\coauther\xiaoyuan\final\clean_data"
	gl final_results           "D:\Prof.Yi\coauther\xiaoyuan\final\results"
	 

*************************************************** Analysis *****************************************************************************************	
/**************************************** Step 1  	Summary statistics ************************************************************************************/
	
use 	$final_cleandata/Analysis.dta,clear

	gl    Y        part part_C part_O
    gl    COMSUM   CountDoc ShareCol ShareAge
	
	gl    DOCSUM   doc_male doc_minority doc_age doc_edulev_1 doc_edulev_2 doc_edulev_3 doc_edulev_4 doc_ForEdu doc_UseInt doc_FullTime doc_PubSer doc_inc   
	gl    FACSUM   fac_Res dis_townhos
	gl    VILSUM   vil_DailyWage

//Tabel 1  Sample Description
	
	
	//Compute Number of Townships
	*ssc install  unique     
	sort vil_code doc_form_code
	
	gen town_code=substr(vil_code,1,4)
	lab var town_code   "Township's ID"

	order county_code town_code vil_code doc_form_code
	destring town_code vil_code,replace
	unique town_code,by(county_code)
	unique vil_code,by(county_code)
	
	
	//Compute Number of Clinicians & NPC
	bys county_code: tabstat  doc_form_code  , statistics(count) columns(variables)
	bys county_code: tabstat  doc_form_code if NPC==1 , statistics(count) columns(variables)

	
	keep if NPC==1   //29 deleted   330-29=301 obs
	

	
//Table 2  Control variables summary statistics 
	 
    eststo clear
	qui eststo: estpost tabstat $DOCSUM $FACSUM $VILSUM  , statistics(mean sd min max count) columns(statistics)
	esttab ///
		using "$final_results/Table2_Characteristics of NPCs, Clinics, and Villages.csv", ///
		title ("Table 2 Characteristics of NPCs, Clinics, and Villages")   ///
		cells ("count mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) max(fmt(%9.2fc)) ") nostar unstack label ///
		nogap noeqlines nonote nolines obslast ///
		replace 


//Table 3  Competition summary statistics 
	 
	tab CountDoc,m 
	
    eststo clear
	qui eststo: estpost tabstat   $COMSUM , statistics(mean sd min max count) columns(statistics)
	esttab ///
		using "$final_results/Table3_ Competition Characteristics.csv", ///
		title ("Table 3 Competition Characteristics")   ///
		cells ("count mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) max(fmt(%9.2fc)) ") nostar unstack label ///
		nogap noeqlines nonote nolines obslast ///
		replace 
	
	
//Table 4 training characteristics


    eststo clear
	qui eststo: estpost tabstat   $Y , statistics(mean sd min max count) columns(statistics)
	esttab ///
		using "$final_results/Table4_ Dependent Variables Characteristics.csv", ///
		title ("Table 4 Dependent Variables Characteristics")   ///
		cells ("count mean(fmt(%9.2fc)) sd(fmt(%9.2fc)) min(fmt(%9.2fc)) max(fmt(%9.2fc)) ") nostar unstack label ///
		nogap noeqlines nonote nolines obslast ///
		replace 

    count if part ==1
	count if part_C ==1
	count if part_O==1
		
		

//column 1 : face to face 
	
	preserve 
	
	reshape long  doc_a3_11 ///
	 doc_a3_9_1  ///
	 doc_a3_15   ///
	 doc_a3_22_01 doc_a3_22_02 doc_a3_22_03 ///
	 doc_a3_22_04 doc_a3_22_05 doc_a3_22_06 doc_a3_22_07 doc_a3_22_08 doc_a3_22_09 ///
	 doc_a3_22_10 doc_a3_22_11 doc_a3_22_12 doc_a3_22_13 doc_a3_22_14 doc_a3_22_15 ///
	 doc_a3_22_16 doc_a3_22_17 doc_a3_22_18 doc_a3_22_19 doc_a3_22_20 doc_a3_22_21 ///
	 doc_a3_22_22 doc_a3_22_23  ///
	 , i(doc_form_code) j(training) string
	 
	recode doc_a3_11 (2=0)
	
	gl  C_FREE doc_a3_11 
	gl  C_TIME doc_a3_9_1
	gl  C_OPER doc_a3_15
	gl  C_CON  doc_a3_22_01 doc_a3_22_02 doc_a3_22_03 ///
	 doc_a3_22_04 doc_a3_22_05 doc_a3_22_06 doc_a3_22_07 doc_a3_22_08 doc_a3_22_09 ///
	 doc_a3_22_10 doc_a3_22_11 doc_a3_22_12 doc_a3_22_13 doc_a3_22_14 doc_a3_22_15 ///
	 doc_a3_22_16 doc_a3_22_17 doc_a3_22_18 doc_a3_22_19 doc_a3_22_20 doc_a3_22_21 ///
	 doc_a3_22_22 doc_a3_22_23 
	
	// Table 4 column 1
	eststo clear
	qui eststo: estpost tabstat $C_FREE $C_TIME $C_OPER $C_CON  if part_C==1 , statistics(count mean) columns(statistics)
	esttab ///
		using "$final_results/table4.facetoface(column1).csv", ///
		cells ("count mean(fmt(%9.2fc))") nostar unstack nolabel ///
		nogap noeqlines nonote nolines obslast ///
		replace

	restore	
	
	
//column 2: web-based	

	
	preserve 	
		
	reshape long  doc_a3_37  ///
	 doc_a3_35    doc_a3_36   ///
	 doc_a3_34_01 doc_a3_34_02 doc_a3_34_03 ///
	 doc_a3_34_04 doc_a3_34_05 doc_a3_34_06 doc_a3_34_07 doc_a3_34_08 doc_a3_34_09 ///
	 doc_a3_34_10 doc_a3_34_11 doc_a3_34_12 doc_a3_34_13 doc_a3_34_14 doc_a3_34_15 ///
	 doc_a3_34_16 doc_a3_34_17 doc_a3_34_18 doc_a3_34_19 doc_a3_34_20 doc_a3_34_21 ///
	 doc_a3_34_22 doc_a3_34_23     ///
	 , i(doc_form_code) j(training) string
	 
	gen o_free=cond(doc_a3_37>0 & doc_a3_37!=.,0,cond(doc_a3_37==0,1,.))
	gen o_time=doc_a3_35 * doc_a3_36 /480    //8h*60min（days）

	gl  O_FREE o_free 
    gl  O_TIME o_time
	gl  O_CON  doc_a3_34_01 doc_a3_34_02 doc_a3_34_03 ///
	 doc_a3_34_04 doc_a3_34_05 doc_a3_34_06 doc_a3_34_07 doc_a3_34_08 doc_a3_34_09 ///
	 doc_a3_34_10 doc_a3_34_11 doc_a3_34_12 doc_a3_34_13 doc_a3_34_14 doc_a3_34_15 ///
	 doc_a3_34_16 doc_a3_34_17 doc_a3_34_18 doc_a3_34_19 doc_a3_34_20 doc_a3_34_21 ///
	 doc_a3_34_22 doc_a3_34_23     

	// Table 4 column 2	
	eststo clear
	qui eststo: estpost tabstat $O_FREE $O_TIME $O_CON if part_O==1 ,statistics(count mean) columns(statistics)   
	esttab ///
		using "$final_results/table4.webbased(column2).csv", ///
		cells ("count mean(fmt(%9.2fc))") nostar unstack nolabel ///
		nogap noeqlines nonote nolines obslast ///
		replace

	restore	
	


**********************************************************************************************************************************************************		
/**************************************** Step 3  	Regression Results ********************************************************************************/


    gl    COMCOU    lnCount 
	gl    COMSHA    ShareCol ShareAge
	
	gl    DOC       doc_male doc_minority doc_age doc_edulev_2 doc_edulev_3 doc_edulev_4 doc_ForEdu doc_UseInt doc_FullTime doc_PubSer lninc
	gl    FAC       lnRes lndis 
	gl    VIL       lnDW


//Table 5  Combined: part  (marginal effect)
		  
	logit part    $COMCOU	                                    i.city_codeid  
	margins, dydx(*) predict(pr) post
	est store  Model1
	
	logit part    $COMSHA                                       i.city_codeid 
	margins, dydx(*) predict(pr) post
	est store Model2

	logit part   $COMCOU $COMSHA                                i.city_codeid 
	margins, dydx(*) predict(pr) post
	est store Model3
	
	logit part   $COMCOU $COMSHA $DOC $FAC $VIL                 i.city_codeid   
	margins, dydx(*) predict(pr) post
	est store Model4
	
	outreg2 [Model1 Model2 Model3 Model4] using $final_results/table5_combined.xls, replace stats(coef se)  level(95) dec(2)  ///
	    title("Table 5 Competition within the Village and Neighboring Villages(Face-to-Face or Wed-based)")  ///
	    sortvar($COMCOU $COMSHA $DOC $FAC $VIL )   /// 
		nodepvar  label     ///
		addnote("Notes: Dependent variable is NPCs’ participation in in-Service trainings (Combined)") ///
	  	drop(i.city_codeid) addtext(Prefecture-level FE, YES) ctitle(margins)
     
	 est drop Model1 Model2  Model3 Model4   

		
//Table 6	Faced to Faced: part_C  (marginal effect)
	
	logit part_C   $COMCOU 		                                i.city_codeid 
	margins, dydx(*) predict(pr) post
	est store  Model1
		
	logit part_C   $COMSHA 	                                    i.city_codeid 
	margins, dydx(*) predict(pr) post
	est store Model2

	logit part_C   $COMCOU $COMSHA 	                            i.city_codeid   
	margins, dydx(*) predict(pr) post
	est store Model3
	
	logit part_C   $COMCOU $COMSHA  $DOC $FAC $VIL              i.city_codeid    
	margins, dydx(*) predict(pr) post
	est store Model4

	outreg2 [Model1 Model2 Model3 Model4] using $final_results/table6_faced.xls, replace stats(coef se)  level(95) dec(2)  ///
	    title("Table 6 Competition within the Village and Neighboring Villages(Face-to-Face)")  ///
	    sortvar($COMCOU $COMSHA	$DOC $FAC $VIL)   /// 
		nodepvar  label     ///
	    addnote("Notes: Dependent variable is NPCs’participation in face-to-face in-service trainings") ///
	  	drop(i.city_codeid) addtext(Prefecture-level FE, YES)  ctitle(margins)
		
	est drop Model1 Model2  Model3 Model4   
	     


//Table 7	Web-based : part_O (marginal effect)
	

    logit part_O     $COMCOU                               i.city_codeid  
	margins, dydx(*) predict(pr) post
	est store  Model1

	logit part_O     $COMSHA 	                           i.city_codeid 
	margins, dydx(*) predict(pr) post
	est store  Model2
	
    logit part_O     $COMCOU $COMSHA	                    i.city_codeid  
	margins, dydx(*) predict(pr) post
	est store  Model3	
		
	logit part_O     $COMCOU $COMSHA  $DOC $FAC $VIL       i.city_codeid  
	margins, dydx(*) predict(pr) post
	est store  Model4

	outreg2 [Model1 Model2 Model3 Model4] using $final_results/table7_web-based.xls, replace stats(coef se)  level(95) dec(2) ///
	    title("Table 7 Competition within the Village and Neighboring Villages(Wed-based)")  ///
	    sortvar($COMCOU $COMSHA $DOC $FAC $VIL)   /// 
		nodepvar  label     ///
	    addnote("Notes: Dependent variable is NPCs’ participation in web-based in-service trainings") ///
	  	drop(i.city_codeid) addtext(Prefecture-level FE, YES) 	ctitle(margins)
		
	 est drop Model1 Model2  Model3 Model4  



///////////////////////////////END
